package com.cipta.lomob;

import org.json.JSONObject;

import tv.lomob.api.Lomob;
import tv.lomob.api.LomobResponseHandler;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import com.cipta.lomob.base.BaseActivity;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;

public class SplashscreenActivity extends BaseActivity {
	private UiLifecycleHelper uiHelper;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_splashscreen);
		LomobManager.getInstance();
		// for facebook hash purpose
		// Add code to print out the key hash

		// start facebook routine
		uiHelper = new UiLifecycleHelper(this, new Session.StatusCallback() {

			@Override
			public void call(Session session, SessionState state,
					Exception exception) {

			}
		});
		uiHelper.onCreate(arg0);

		new Delay().execute();
	}

	@Override
	protected void onPause() {
		super.onPause();
		uiHelper.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		uiHelper.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
		uiHelper.onStop();
	}

	private class Delay extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				// TODO: handle exception
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// this check if login session is valid
			if (isSessionValid()) {
				// ConnectFriendsActivity.toConnectFriendsActivity(SplashscreenActivity.this);
				// check if this user exist on server
				JSONObject user = LomobManager.getInstance().getUser(
						SplashscreenActivity.this);
				Lomob.authentificationFacebook(user.optString("facebook_id"),
						user.optString("token"), new LomobResponseHandler() {

							@Override
							public void onSuccess(JSONObject result) {
								MainActivity
										.toMainActivity(SplashscreenActivity.this);
							}

							@Override
							public void onError(String message) {
								Session.getActiveSession()
										.closeAndClearTokenInformation();
								RegisterActivity
										.toRegisterActivity(SplashscreenActivity.this);
							}
						});
			} else {
				RegisterActivity.toRegisterActivity(SplashscreenActivity.this);
			}
			finish();
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		uiHelper.onActivityResult(requestCode, resultCode, data);
	}

	/*
	 * this function below for authentification
	 */

	private boolean isSessionValid() {
		// get facebook session
		Session session = Session.getActiveSession();
		if (session.isOpened()) {
			return true;
		}
		return false;
	}
}
